package com.stylefeng.guns.modular.huamao.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.huamao.dao.GoodsSpecsMapper;
import com.stylefeng.guns.modular.huamao.model.GoodsSpecs;
import com.stylefeng.guns.modular.huamao.model.SpecItems;
import com.stylefeng.guns.modular.huamao.service.IGoodsSpecsService;
import com.stylefeng.guns.modular.huamao.service.ISpecItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 商品规格表 服务实现类
 * </p>
 *
 * @author wudi123
 * @since 2018-04-10
 */
@Service
public class GoodsSpecsServiceImpl extends ServiceImpl<GoodsSpecsMapper, GoodsSpecs> implements IGoodsSpecsService {
    @Autowired
    private ISpecItemsService specItemsService;
    @Override
    @Transactional
    public Map<String,Object> addGoodsSpecsAndSpecItems(String stringList){
        Map<String,Object> map=new HashMap<>();
        try{
//            List  catIdList=new ArrayList();
            List<SpecItems> list=new ArrayList();
            JSONObject jsonObject = JSONObject.parseObject(stringList);
            JSONArray jsonArray = JSONArray.parseArray(jsonObject.get("goodsSpec").toString());
            JSONArray catIdJsonArray = JSONArray.parseArray(jsonObject.get("catId").toString());
//            if (catIdJsonArray!=null && catIdJsonArray.size()>0){
//
//                for (int i=0;i<catIdJsonArray.size();i++){
//                    JSONObject jsonCatId = JSONObject.parseObject(catIdJsonArray.get(i).toString());
//                    catIdList.add(jsonCatId.get("catId").toString());
//                    ;
//
//                }
//            }
            String catId1 = jsonObject.get("catId").toString();
            catId1=catId1.substring(1,catId1.length()-1);
            String[] catIdList = catId1.split(",");
            JSONArray itemsName2 = JSONArray.parseArray(jsonObject.get("itemsName").toString());
            Integer shopId = Integer.parseInt(jsonObject.get("shopId").toString());
            Integer goodsId = Integer.parseInt(jsonObject.get("goodsId").toString());
            SpecItems specItem=new SpecItems();
            specItem.setGoodsId(goodsId);
            EntityWrapper<SpecItems> entityWrapper1 =new EntityWrapper<SpecItems>(specItem);
            specItemsService.delete(entityWrapper1);
            GoodsSpecs goodsSpec=new GoodsSpecs();
            goodsSpec.setGoodsId(goodsId);
            EntityWrapper<GoodsSpecs> entityw =new EntityWrapper<GoodsSpecs>(goodsSpec);
            this.delete(entityw);
            if (catIdList!=null && catIdList.length>0){
                for (int i=0;i<catIdList.length;i++){
                    JSONObject jsonObject1 = JSONObject.parseObject(itemsName2.get(i).toString());
                    String s = jsonObject1.get(catIdList[i]).toString();
                    s=s.substring(1,s.length()-1);
                    String[] itemsNameList = s.split(",");

                    for (String itemsName:itemsNameList){
                        SpecItems specItems=new SpecItems();
                        specItems.setCatId(Integer.parseInt(catIdList[i]));
                        specItems.setCreateTime(new Date());
                        specItems.setDataFlag(1);
                        specItems.setItemName(itemsName.substring(1,itemsName.length()-1));
                        specItems.setGoodsId(goodsId);
                        specItems.setItemImg("");
                        specItems.setShopId(shopId);
                        list.add(specItems);
                    }
                }
                specItemsService.insertBatch(list);
            }
            List<GoodsSpecs> GoodsSpecsList=new ArrayList();
            for (int j=0;j<jsonArray.size();j++){
                JSONObject jsonObject1 = JSONObject.parseObject(jsonArray.get(j).toString());
                String productNo = jsonObject1.get("productNo").toString();
                String marketPrice =jsonObject1.get("marketPrice").toString();
                String specPrice =jsonObject1.get("specPrice").toString();
                String specStock =jsonObject1.get("specStock").toString();
                String warnStock = jsonObject1.get("warnStock").toString();
                String saleNum =jsonObject1.get("saleNum").toString();
                String specIds="";
                for (int i=0;i<catIdList.length;i++){
                    Integer catId=Integer.parseInt(catIdList[i]);
                    String itemName = jsonObject1.get(catId).toString();
                    SpecItems specItems=new SpecItems();
                    specItems.setCatId(catId);
                    specItems.setItemName(itemName);
                    specItems.setGoodsId(goodsId);
                    specItems.setShopId(shopId);
                    EntityWrapper<SpecItems> entityWrapper =new EntityWrapper<SpecItems>(specItems);
                    SpecItems specItems1 = specItemsService.selectOne(entityWrapper);
                    Integer specId=specItems1.getItemId();
                    specIds=specIds+specId+":";
                }
                specIds=specIds.substring(0,specIds.length()-1).toString();
                GoodsSpecs goodsSpecs=new GoodsSpecs();
                goodsSpecs.setShopId(shopId);
                goodsSpecs.setGoodsId(goodsId);
                goodsSpecs.setProductNo(productNo);
                goodsSpecs.setSpecIds(specIds);
                goodsSpecs.setMarketPrice(new BigDecimal(marketPrice));
                goodsSpecs.setSpecPrice(new BigDecimal(specPrice));
                goodsSpecs.setSpecStock(Integer.parseInt(specStock));
                goodsSpecs.setWarnStock(Integer.parseInt(warnStock));
                goodsSpecs.setSaleNum(Integer.parseInt(saleNum));
                goodsSpecs.setDataFlag(1);
                GoodsSpecsList.add(goodsSpecs);

            }
            this.insertBatch(GoodsSpecsList);
            map.put("code","01");
            map.put("msg","操作成功");
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            map.put("code","00");
            map.put("msg","操作失败");
        }

        return map;
    }
}
